package main;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import dao.CommDAO;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class Room extends JFrame {

	private CommDAO dao = new CommDAO();
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	JComboBox comboBox = new JComboBox();

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Room frame = new Room();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Room() {
		setTitle("设备信息编辑");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 575, 435);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("设备ID:");
		lblNewLabel.setBounds(120, 38, 54, 15);
		contentPane.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(173, 35, 139, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.setBounds(322, 34, 73, 23);
		contentPane.add(btnNewButton);
		
		JLabel lblNewLabel_1 = new JLabel("设备ID:");
		lblNewLabel_1.setBounds(163, 81, 54, 15);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_1_1 = new JLabel("门牌号：");
		lblNewLabel_1_1.setBounds(163, 124, 54, 15);
		contentPane.add(lblNewLabel_1_1);
		
		JLabel lblNewLabel_1_1_1 = new JLabel("楼层：");
		lblNewLabel_1_1_1.setBounds(163, 167, 54, 15);
		contentPane.add(lblNewLabel_1_1_1);
		
		JLabel lblNewLabel_1_1_1_1 = new JLabel("设备类型：");
		lblNewLabel_1_1_1_1.setBounds(163, 209, 54, 15);
		contentPane.add(lblNewLabel_1_1_1_1);
		
		JLabel lblNewLabel_1_1_1_1_1 = new JLabel("设备价格：");
		lblNewLabel_1_1_1_1_1.setBounds(163, 258, 54, 15);
		contentPane.add(lblNewLabel_1_1_1_1_1);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(209, 78, 139, 21);
		contentPane.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(209, 121, 139, 21);
		contentPane.add(textField_2);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(209, 164, 139, 21);
		contentPane.add(textField_3);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(209, 206, 139, 21);
		contentPane.add(textField_4);
		
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setBounds(209, 255, 139, 21);
		contentPane.add(textField_5);
		
		JButton btnNewButton_1 = new JButton("新增");
		btnNewButton_1.setBounds(163, 348, 73, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_1_1 = new JButton("修改");
		btnNewButton_1_1.setBounds(246, 348, 73, 23);
		contentPane.add(btnNewButton_1_1);
		
		JLabel lblNewLabel_1_1_1_1_1_1 = new JLabel("设备价格：");
		lblNewLabel_1_1_1_1_1_1.setBounds(163, 301, 54, 15);
		contentPane.add(lblNewLabel_1_1_1_1_1_1);
		
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"未录入", "已录入"}));
		comboBox.setBounds(209, 298, 139, 21);
		contentPane.add(comboBox);
		
		JButton btnNewButton_1_1_1 = new JButton("关闭");
		btnNewButton_1_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnNewButton_1_1_1.setBounds(322, 348, 73, 23);
		contentPane.add(btnNewButton_1_1_1);
		
		

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setDefaultValue();
				String sql = "select * from room where id = '" + textField.getText() + "'";
				List<Map<String,Object>> list = dao.select(sql);
				if(list.size() > 0) {
					Map<String, Object> map = list.get(0);
					textField_1.setText(map.get("id").toString());
					textField_2.setText(map.get("num").toString());
					textField_3.setText(map.get("floor").toString());
					textField_4.setText(map.get("type").toString());
					textField_5.setText(map.get("price").toString());
					if("已入住".equals(map.get("state")))
						comboBox.setSelectedIndex(1);
				}
			}
		});
		
		
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sql = "insert into room values('" 
						+ textField_1.getText() + "', '"
						+ textField_2.getText()  + "', '"
						+ textField_3.getText()  + "', '"
						+ comboBox.getSelectedItem().toString() + "', '"
						+ textField_4.getText()  + "', '"
						+ textField_5.getText()
						+ "')";
				System.out.println(sql);
				dao.commOper(sql);
			}
		});
		
		btnNewButton_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sql = "update room set "
						+ " num = '" + textField_2.getText() + "', "
						+ " floor = '" + textField_3.getText() + "', "
						+ " state = '" + comboBox.getSelectedItem().toString() + "', "
						+ " type = '" + textField_4.getText() + "', "
						+ " price = '" + textField_5.getText() + "'"
						+ " where id = " + textField_1.getText();
				System.out.println(sql);
				dao.commOper(sql);
			}
		});
	}
	
	public void setDefaultValue() {
		textField_1.setText("");
		textField_2.setText("");
		textField_3.setText("");
		textField_4.setText("");
		textField_5.setText("");
		comboBox.setSelectedIndex(0);
	}
}
